﻿/**************************************************************************************
 * Logger.cs
 * An utility class to log exceptions and messages into the sharepoint server log.
 * Jaime Alberto Jaramillo Zapata (jjaramillo.informatica-tecnologia.com)
 * Created on 06/02/2012.
 * Last update on 06/02/2012.
 **************************************************************************************/

using System;
using Microsoft.SharePoint.Administration;
using MS.InformationManagement.Support.Enums;

namespace MS.InformationManagement.Support
{
    /// <summary>
    /// An utility class to log exceptions and messages into the sharepoint server log.
    /// </summary>
    public class Logger
    {
        /// <summary>
        /// Logs an exception into the sharepoint server log.
        /// </summary>
        /// <param name="ex">The exception which contains the information to be logged.</param>
        /// <param name="eventSeverity">The event severity.</param>
        /// <param name="diagnosticsAreaName">Name of the diagnostics area which contais the diagnostics category.</param>
        /// <param name="diagnosticsAreaCategoryName">Name of the diagnostics area category.</param>
        public static void LogException(Exception ex, EventSeverity eventSeverity, string diagnosticsAreaName, string diagnosticsAreaCategoryName)
        {
           // SPDiagnosticsService currentLogService = SPDiagnosticsService.Local;
           // SPDiagnosticsCategory eventCategory = currentLogService.Areas[diagnosticsAreaName].Categories[diagnosticsAreaCategoryName];
            //TODO: Define the message body for the exception
           // currentLogService.WriteEvent(0,
           //     eventCategory, eventSeverity, "An exception has occurred in {0} : {1}. More information can be found on the stacktrace: {2}",
           //     new object[] { ex.Source, ex.Message, ex.StackTrace });
        }

        /// <summary>
        /// Logs a message string into the sharepoint server log.
        /// </summary>
        /// <param name="message">The message to be logged into the sharepoint server log.</param>
        /// <param name="diagnosticsAreaName">Name of the diagnostics area which contais the diagnostics category.</param>
        /// <param name="diagnosticsAreaCategoryName">Name of the diagnostics area category.</param>
        public static void LogMessage(string message, string diagnosticsAreaName, string diagnosticsAreaCategoryName)
        {
            SPDiagnosticsService currentLogService = SPDiagnosticsService.Local;
            SPDiagnosticsCategory eventCategory = currentLogService.Areas[diagnosticsAreaName].Categories[diagnosticsAreaCategoryName];
            //TODO: Define message body structure for the message
            currentLogService.WriteEvent(0,
                eventCategory, EventSeverity.Information, "Logging Message: {0}", new object[] { message });
        }
    }
}
